package cn.ulc.socket.weapp.service.impl;

import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.util.CalendarUtil;
import cn.ulc.socket.weapp.common.BaseService;
import cn.ulc.socket.weapp.model.dto.GoodsDTO;
import cn.ulc.socket.weapp.model.dto.GoodsListDTO;
import cn.ulc.socket.weapp.model.dto.GoodsSkuDTO;
import cn.ulc.socket.weapp.model.dto.GoodsTypeListDTO;
import cn.ulc.socket.weapp.service.GoodsService;
import cn.ulc.store.dao.goods.GoodsDao;
import cn.ulc.store.dao.goods.GoodsSpecDao;
import cn.ulc.store.dao.goods.GoodsTypeDao;
import cn.ulc.store.dao.user.UserFavoriteGoodsDao;
import cn.ulc.store.entity.Company;
import cn.ulc.store.entity.goods.Goods;
import cn.ulc.store.entity.goods.GoodsSpec;
import cn.ulc.store.entity.goods.GoodsType;
import cn.ulc.store.entity.user.UserFavoriteGoods;
import cn.ulc.store.entity.user.WeixinUser;
import lombok.AllArgsConstructor;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class GoodsServiceImpl extends BaseService implements GoodsService {

    GoodsTypeDao typeDao;
    GoodsDao goodsDao;
    GoodsSpecDao specDao;
    UserFavoriteGoodsDao favoriteGoodsDao;
    @Override
    public GoodsSkuDTO findSku(Long goodsId) {
        Company company = getCurrentCompany();
        List<GoodsSpec> specList = specDao.findByGoodsId(company.getId(),goodsId);
        return new GoodsSkuDTO(specList);
    }

    @Override
    public GoodsTypeListDTO findTypeList(Long id) {
        Company loginCompany = getCurrentCompany();
        List<GoodsType> goodsTypeList = typeDao.findListGoodsCount(loginCompany.getId(), AvailableStatus.AVAILABLE);
        Map<Integer,List<GoodsType>> map = goodsTypeList.stream().collect(Collectors.groupingBy(GoodsType::getLevel));
        List<GoodsType> firstTypeList = map.get(1);
        List<GoodsType> typeList = map.get(2);
        List<GoodsType> secondTypeList = new ArrayList<>();
        if(typeList !=null && typeList.size()>0){
            for(GoodsType type:typeList){
                if(type.getGoodsCount()>0) secondTypeList.add(type);
            }
        }
        GoodsTypeListDTO dto = new GoodsTypeListDTO();
        dto.setFirstTypeList(firstTypeList);
        dto.setSecondTypeList(secondTypeList);
        //查询新品
        if(firstTypeList !=null && firstTypeList.size()>0) {
            if (id.intValue() == 0) {
                id = firstTypeList.get(0).getId();
            }
            String date = CalendarUtil.dateToString(CalendarUtil.afterDays(new Date(),-7),CalendarUtil.SDF_DEFAULT_FULL);
            List<Goods> goodsList = goodsDao.findByParentTypeId(loginCompany.getId(), id,AvailableStatus.AVAILABLE, date);
            dto.setGoodsList(goodsList);
        }
        return dto;
    }

    @Override
    public GoodsListDTO findNewGoodsList(Long id) {
        Company loginCompany = getCurrentCompany();
        String date = CalendarUtil.dateToString(CalendarUtil.afterDays(new Date(),-7),CalendarUtil.SDF_DEFAULT_FULL);
        List<Goods> goodsList = goodsDao.findByParentTypeId(loginCompany.getId(), id,AvailableStatus.AVAILABLE, date);
        return new GoodsListDTO(goodsList,null);
    }

    @Override
    public GoodsTypeListDTO findTypeListByParentId(Long id) {
        Company loginCompany = getCurrentCompany();
        List<GoodsType> goodsTypeList = typeDao.findListByParentId(loginCompany.getId(), AvailableStatus.AVAILABLE,id);
        GoodsTypeListDTO dto = new GoodsTypeListDTO();
        dto.setSecondTypeList(goodsTypeList);
        return dto;
    }

    @Override
    public GoodsListDTO findList(Long typeId,String searchKey, PaginationParam param) {
        Company loginCompany = getCurrentCompany();
        Map<String,Object> condition = new HashMap<>();
        condition.put("companyId",loginCompany.getId());
        condition.put("goodsTypeId",typeId);
        condition.put("key",searchKey);
        PageQuery pageQuery = initPageQuery(param);
        pageQuery.setParas(condition);
        goodsDao.findWxListPage(pageQuery);
        return new GoodsListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public GoodsDTO findDetail(String barCode) {
        WeixinUser loginUser = getCurrentUser();
        Company company = getCurrentCompany();
        Goods goods = goodsDao.findByBarCode(company.getId(),barCode);
        if(goods==null) throw new PlatformException("商品信息获取失败!");
        List<GoodsSpec> specList = specDao.findByGoodsId(goods.getCompanyId(),goods.getId());
        UserFavoriteGoods favoriteGoods = favoriteGoodsDao.findByGoodsId(goods.getId(),loginUser.getId());

        return new GoodsDTO(goods,specList,favoriteGoods);
    }
}
